/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tongxunlu;

import java.io.*;
import java.util.ArrayList;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Tim
 */
public class MainMenu extends javax.swing.JFrame {

    /**
     * Creates new form MainMenu
     */
    public MainMenu() {
        String[] string = {"姓名","电话","邮箱","生日","分组"};
        n = new DefaultTableModel(null,new String[]{"姓名","电话","邮箱","生日","分组"});
        initComponents();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooser1 = new javax.swing.JFileChooser();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList(m);
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        AddButton = new javax.swing.JButton();
        DeleButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        ModifyjButton = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jSeparator1.setBounds(0, 100, 970, 2);
        jLayeredPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        jScrollPane1.setBounds(0, 140, 180, 360);
        jLayeredPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTable1.setModel(n);
        jScrollPane2.setViewportView(jTable1);

        jScrollPane2.setBounds(190, 100, 780, 410);
        jLayeredPane1.add(jScrollPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        AddButton.setText("添加联系人");
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });
        AddButton.setBounds(30, 20, 93, 60);
        jLayeredPane1.add(AddButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        DeleButton.setText("删除联系人");
        DeleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleButtonActionPerformed(evt);
            }
        });
        DeleButton.setBounds(610, 20, 93, 60);
        jLayeredPane1.add(DeleButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setFont(new java.awt.Font("微软雅黑", 1, 18)); // NOI18N
        jLabel1.setText("查询：");
        jLabel1.setBounds(0, 110, 60, 24);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField1KeyReleased(evt);
            }
        });
        jTextField1.setBounds(60, 110, 120, 30);
        jLayeredPane1.add(jTextField1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton1.setText("详细信息");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.setBounds(870, 60, 81, 23);
        jLayeredPane1.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        ModifyjButton.setText("修改联系人");
        ModifyjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ModifyjButtonActionPerformed(evt);
            }
        });
        ModifyjButton.setBounds(320, 20, 100, 60);
        jLayeredPane1.add(ModifyjButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton2.setText("刷新");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jButton2.setBounds(791, 60, 60, 23);
        jLayeredPane1.add(jButton2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jMenu1.setText("文件操作");
        jMenu1.setFont(new java.awt.Font("微软雅黑", 0, 14)); // NOI18N

        jMenu3.setText("导入文件");

        jMenuItem1.setText("导入CSV格式文件");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem1);

        jMenuItem2.setText("导入VCard格式文件");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem2);

        jMenu1.add(jMenu3);

        jMenu4.setText("导出文件");

        jMenuItem3.setText("导出CSV格式文件");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem3);

        jMenuItem4.setText("导出VCard格式文件");
        jMenu4.add(jMenuItem4);

        jMenu1.add(jMenu4);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 966, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed
        AddPerson addperson = new AddPerson(null,true);
        addperson.setVisible(true);
        addperson.Addp();// TODO add your handling code here:   
    }//GEN-LAST:event_AddButtonActionPerformed

    private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyReleased
       String str1 = jTextField1.getText();
                if (str1.equals(m_str)) {
                    return;
                }
                m_str = str1;
                function ftion = new function();
                main = ftion.mohusearch(personList, m_str);
                

                int len = (n.getRowCount());
                for (int i = 0; i < len; i++) {

                    n.removeRow(0);
                }
                String[] print = new String[5];
                System.out.println(main.size());
                for (int i = 0; i < main.size(); i++) {
                    print[0] = ((person) main.get(i)).getNAME();
                    print[1] = ((person) main.get(i)).getTEL();
                    print[2] = ((person) main.get(i)).getEMAIL();
                    print[3] = ((person) main.get(i)).getBIR();
                    print[4] = ((person) main.get(i)).getGROUP();
                    
                    n.addRow(print);

                } // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1KeyReleased

    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
        strname = (String) (m.getElementAt(jList1.getSelectedIndex()));//获取组名
                System.out.println(strname + "111");
                ArrayList ay = new ArrayList();
                function f1 = new function();
                main.clear();
                main = f1.divgroup2(personList, strname);

                int len = (n.getRowCount());
                for (int i = 0; i < len; i++) {
                    n.removeRow(0);
                }

                String[] print = new String[5];
                for (int i = 0; i < ay.size(); i++) {
                    print[0] = ((person) main.get(i)).getNAME();
                    print[1] = ((person) main.get(i)).getTEL();
                    print[2] = ((person) main.get(i)).getEMAIL();
                    print[3] = ((person) main.get(i)).getBIR();
                    print[4] = ((person) main.get(i)).getGROUP();
                    
                    n.addRow(print);
                }
                int len1 = (n.getRowCount());
        
            for (int i = 0; i < len1; i++) {
                n.removeRow(0);
            }
            if (jList1.getSelectedIndex()==-1 || strname.equals("全部")) {
                String[] print1 = new String[5];
                for (int i = 0; i < personList.size(); i++) {
                    print1[0] = ((person) personList.get(i)).getNAME();
                    print1[1] = ((person) personList.get(i)).getTEL();
                    print1[2] = ((person) personList.get(i)).getEMAIL();
                    print1[3] = ((person) personList.get(i)).getBIR();
                    print1[4] = ((person) personList.get(i)).getGROUP();
                    
                    n.addRow(print1);
                }
            } else {
                function f2 = new function();
                ArrayList<person> arr = new ArrayList<person>();
                arr = f2.divgroup2(personList, strname); 
                String[] print1 = new String[5];
                for (int i = 0; i < arr.size(); i++) {
                    print1[0] = ((person) arr.get(i)).getNAME();
                    print1[1] = ((person) arr.get(i)).getTEL();
                    print1[2] = ((person) arr.get(i)).getEMAIL();
                    print1[3] = ((person) arr.get(i)).getBIR();
                    print1[4] = ((person) arr.get(i)).getGROUP();
                    n.addRow(print1);
                }
            }
                // TODO add your handling code here:
    }//GEN-LAST:event_jList1MouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int i = jTable1.getSelectedRow();
        function f = new function();
        person p = new person();
        if(i!= -1){
            p.setNAME(n.getValueAt(i, 0).toString());
            p.setTEL(n.getValueAt(i, 1).toString());
            p.setEMAIL(n.getValueAt(i, 2).toString());
            p.setBIR(n.getValueAt(i, 3).toString());
            p.setGROUP(n.getValueAt(i, 4).toString());
            p = personList.get(f.March(personList, p));
            AddPerson addperson = new AddPerson(p,false);
            addperson.setVisible(true);
            addperson.More();
            
        }
        
            ;// TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void ModifyjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ModifyjButtonActionPerformed
        int i = jTable1.getSelectedRow();
        person p = new person();
        if(i!= -1){
            p.setNAME(n.getValueAt(i, 0).toString());
            p.setTEL(n.getValueAt(i, 1).toString());
            p.setEMAIL(n.getValueAt(i, 2).toString());
            p.setBIR(n.getValueAt(i, 3).toString());
            p.setGROUP(n.getValueAt(i, 4).toString());
            function f = new function();
//            p = personList.get(f.March(personList, p));
            AddPerson addperson = new AddPerson(personList.get(f.March(personList, p)),true);
            addperson.setVisible(true);
            addperson.Modify();
        }// TODO add your handling code here:
    }//GEN-LAST:event_ModifyjButtonActionPerformed

    private void DeleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleButtonActionPerformed
        int j = jTable1.getSelectedRow();
        person p = new person();
        if(j!= -1){
            p.setNAME(n.getValueAt(j, 0).toString());
            p.setTEL(n.getValueAt(j, 1).toString());
            p.setEMAIL(n.getValueAt(j, 2).toString());
            p.setBIR(n.getValueAt(j, 3).toString());
            p.setGROUP(n.getValueAt(j, 4).toString());
            function f = new function();
            int index = f.March(personList, p);
            main.remove(personList.get(index));
            personList.remove(index);
            
            
            int len = (n.getRowCount());
        if (m != null) {
            m.clear();
        }
        for (int i = 0; i < len; i++) {
            n.removeRow(0);
        }
        if (jList1.getSelectedIndex() == -1 || strname.equals("全部")) {
            String[] print = new String[5];
            for (int i = 0; i < personList.size(); i++) {
                print[0] = ((person) personList.get(i)).getNAME();
                print[1] = ((person) personList.get(i)).getTEL();
                print[2] = ((person) personList.get(i)).getEMAIL();
                print[3] = ((person) personList.get(i)).getBIR();
                print[4] = ((person) personList.get(i)).getGROUP();

                n.addRow(print);
            }
        } else {
            function f2 = new function();
            ArrayList<person> arr = new ArrayList<person>();
            arr = f2.divgroup2(personList, strname);
            String[] print = new String[5];
            for (int i = 0; i < arr.size(); i++) {
                print[0] = ((person) arr.get(i)).getNAME();
                print[1] = ((person) arr.get(i)).getTEL();
                print[2] = ((person) arr.get(i)).getEMAIL();
                print[3] = ((person) arr.get(i)).getBIR();
                print[4] = ((person) arr.get(i)).getGROUP();
                n.addRow(print);
            }
        }
        function f2 = new function();
        groupname = f2.divgroup(personList);   //分组名称;
        for (int k = 0; k < groupname.length; k++) {
            m.add(0, groupname[k]);

        }
        m.add(0, "全部");// TODO add your handling code here:
    }
            
        // TODO add your handling code here:
    }//GEN-LAST:event_DeleButtonActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        int len = (n.getRowCount());
        if (m != null) {
            m.clear();
        }
        for (int i = 0; i < len; i++) {
            n.removeRow(0);
        }
        if (jList1.getSelectedIndex() == -1 || strname.equals("全部")) {
            String[] print = new String[5];
            for (int i = 0; i < personList.size(); i++) {
                print[0] = ((person) personList.get(i)).getNAME();
                print[1] = ((person) personList.get(i)).getTEL();
                print[2] = ((person) personList.get(i)).getEMAIL();
                print[3] = ((person) personList.get(i)).getBIR();
                print[4] = ((person) personList.get(i)).getGROUP();

                n.addRow(print);
            }
        } else {
            function f2 = new function();
            ArrayList<person> arr = new ArrayList<person>();
            arr = f2.divgroup2(personList, strname);
            String[] print = new String[5];
            for (int i = 0; i < arr.size(); i++) {
                print[0] = ((person) arr.get(i)).getNAME();
                print[1] = ((person) arr.get(i)).getTEL();
                print[2] = ((person) arr.get(i)).getEMAIL();
                print[3] = ((person) arr.get(i)).getBIR();
                print[4] = ((person) arr.get(i)).getGROUP();
                n.addRow(print);
            }
        }
        function f = new function();
        groupname = f.divgroup(personList);   //分组名称;
        for (int j = 0; j < groupname.length; j++) {
            m.add(0, groupname[j]);

        }
        m.add(0, "全部");// TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "CSV Files", "csv");
        chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File f = chooser.getSelectedFile();
            System.out.println("You chose to open this file: "
                    + chooser.getSelectedFile().getName());
            personList.clear();
            if(CSVOP.CSVReader(f.getAbsolutePath(), personList)){JOptionPane.showMessageDialog(null, "文件导入成功！请按刷新！");
            Collections.sort(personList, new SpellComparator());
            }
        }
        
// TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "CSV Files", "csv");
        chooser.setFileFilter(filter);
        int returnVal = chooser.showSaveDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File f = chooser.getSelectedFile();
            System.out.println("You chose to open this file: "
                    + chooser.getSelectedFile().getName());
            try {
                if(CSVOP.CSVWriter(f.getAbsolutePath(), personList))JOptionPane.showMessageDialog(null ,"文件成功导出！");
            } catch (IOException ex) {
                Logger.getLogger(MainMenu.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else JOptionPane.showMessageDialog(null, "Error!");// TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
               MainMenu mainmenu = new MainMenu();
               mainmenu.setVisible(true);
               mainmenu.setResizable(false);
                
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddButton;
    private javax.swing.JButton DeleButton;
    private javax.swing.JButton ModifyjButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JList jList1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
    public static ArrayList<person> personList=new ArrayList<person>();
    public static ArrayList main = new ArrayList(); 
    public static DefaultListModel m=new DefaultListModel();
    public static DefaultTableModel n;
    private String m_str;
    String[] groupname;
    public static String strname;
    public  void NewTable(){
                
    }
        
}
